package pri.wallen.pipedStream;

import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;

/**
 * 项目名称：DaemonThreadLab
 * 类名称：Piped
 * 类描述：
 * 创建人：Wallen Han
 * 创建时间：2018年03月21日 17:09
 */
public class Piped {

    public static void main (String[] args) throws IOException {
        PipedReader  in = new PipedReader();
        PipedWriter out = new PipedWriter();

        in.connect(out);
        Thread printThread = new Thread(new Print(in), "printTread");
        printThread.start();
        int recieve = 0;
        try {
            while ((recieve = System.in.read()) != -1) {
                out.write(recieve);
            }
        } finally {
            out.close();
        }

    }

    static class Print implements  Runnable {
        private PipedReader in;

        public Print (PipedReader in) {
            this.in =in;
        }

        @Override
        public void run() {
            int recieve = 0;
            try {
                while ((recieve = in.read()) != -1) {
                    System.out.print((char) recieve);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
